/*************************************************************************
    > File Name: main.cpp
    > Author: Jintao Yang
    > Mail: 18608842770@163.com 
    > Created Time: 2020年11月26日 星期四 09时07分46秒
 ************************************************************************/

#include <gtk/gtk.h>
#include "rd905uw.h"
#include<vector>

using namespace std;

rd905uw *rdobj = nullptr;

gchar entry_port[32] = "/dev/ttyUSB0";
gchar entry_baud[16] = "57600";

static void
open_callback(GtkWidget *widget, gpointer data)
{
	if (nullptr != rdobj)
	{
		int ret = rdobj->open_device((std::string)entry_port, atoi(entry_baud));
		printf("open ret: %d\n", ret);
	}
}

static void
close_callback(GtkWidget *widget, gpointer data)
{
	if (nullptr != rdobj)
	{
		int ret = rdobj->close_device();
		printf("close ret: %d\n", ret);
	}
}

static void 
cbt_port_callback(GtkWidget *widget, gpointer data)
{
	memset(entry_port, 0, sizeof(entry_port));
	strcpy(entry_port, gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget)));
	g_print("entry_port content:%s\n", entry_port);
}

static void 
cbt_baud_callback(GtkWidget *widget, gpointer data)
{
	memset(entry_baud, 0, sizeof(entry_baud));
	strcpy(entry_baud, gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget)));
	g_print("entry_baud content:%s\n", entry_baud);
}

static void
print_EPC(GtkWidget *widget,
			gpointer data)
{
	if (nullptr != rdobj)
	{
		//memset(data, 0, sizeof(data));
		char szEPC[128] = "\0";
		memset(szEPC, 0, sizeof(szEPC));
		if (rdobj->querryEPC(szEPC) > 0)
			gtk_label_set_text(GTK_LABEL(data), szEPC);
		g_print("EPC: %s\n", szEPC);
	}
}

static void
print_TID(GtkWidget *widget,
			gpointer data)
{
	if (nullptr != rdobj)
	{
		//memset(data, 0, sizeof(data));
		char szTID[128] = "\0";
		memset(szTID, 0, sizeof(szTID));
		if (rdobj->querryTID(szTID) > 0)
			gtk_label_set_text(GTK_LABEL(data), szTID);
		g_print("TID: %s\n", szTID);
	}
}

static void
clear_epc(GtkWidget *widget,
		  gpointer data)
{
	gtk_label_set_text(GTK_LABEL(data), "");
}

static void
clear_tid(GtkWidget *widget,
		  gpointer data)
{
	gtk_label_set_text(GTK_LABEL(data), "");
}

int main(int argc, char **argv)
{
	GtkBuilder *builder = NULL;
	GtkWidget *win = NULL;
	GObject *window = NULL;
	GObject *button = NULL;
	GObject *comboBoxText = NULL;
	GObject *label_tid = NULL;
	GObject *label_epc = NULL;
	GError *error = NULL;

	rdobj = new rd905uw();

	gtk_init(&argc, &argv);

	builder = gtk_builder_new();
	if (gtk_builder_add_from_file(builder, "/usr/local/bin/builder.ui", &error) == 0)
	{
		g_printerr("Error loading file: %s\n", error->message);
		g_clear_error(&error);
		return 1;
	}

	window = gtk_builder_get_object(builder, "window_main");
	g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

	label_epc = gtk_builder_get_object(builder, "label_epc");
	label_tid = gtk_builder_get_object(builder, "label_tid");

	button = gtk_builder_get_object(builder, "btn_get_epc");
	g_signal_connect(button, "clicked", G_CALLBACK(print_EPC), (gpointer)label_epc);

	button = gtk_builder_get_object(builder, "btn_get_tid");
	g_signal_connect(button, "clicked", G_CALLBACK(print_TID), (gpointer)label_tid);

	button = gtk_builder_get_object(builder, "btn_open");
	g_signal_connect(button, "clicked", G_CALLBACK(open_callback), NULL);

	button = gtk_builder_get_object(builder, "btn_close");
	g_signal_connect(button, "clicked", G_CALLBACK(close_callback), NULL);

	button = gtk_builder_get_object(builder, "btn_clear_epc");
	g_signal_connect(button, "clicked", G_CALLBACK(clear_epc), (gpointer)label_epc);

	button = gtk_builder_get_object(builder, "btn_clear_tid");
	g_signal_connect(button, "clicked", G_CALLBACK(clear_tid), (gpointer)label_tid);

	comboBoxText = gtk_builder_get_object(builder, "cbt_port");
	//entry_port = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(comboBoxText));
	gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(comboBoxText));
	vector<serial::PortInfo> devices_found = serial::list_ports();
	vector<serial::PortInfo>::iterator iter = devices_found.begin();
	int i = 0;
	while (iter != devices_found.end())
	{
		serial::PortInfo device = *iter++;
		gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(comboBoxText), to_string(i).c_str(), device.port.c_str());
		
		i++;
		printf("%d. Port - <%s>\n\tDescription: %s\n\tHardware_id: %s\n\n", i, device.port.c_str(), device.description.c_str(), device.hardware_id.c_str());
	}
	printf("init entry port:%s\n", entry_port);
	g_signal_connect(comboBoxText, "changed", G_CALLBACK(cbt_port_callback), NULL);

	comboBoxText = gtk_builder_get_object(builder, "cbt_baud");
	memset(entry_baud, 0, sizeof(entry_baud));
	strcpy(entry_baud, gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(comboBoxText)));
	printf("init entry baud:%s\n", entry_baud);
	g_signal_connect(comboBoxText, "changed", G_CALLBACK(cbt_baud_callback), NULL);

	g_object_unref(builder);

	gtk_widget_show(GTK_WIDGET(window));
	gtk_main();

	if (nullptr != rdobj)
	{
		delete rdobj;
		rdobj = nullptr;
	}

	return 0;
}
